package com.project.platform.service.impl;

import com.project.platform.entity.CheckinApplication;
import com.project.platform.entity.Elder;
import com.project.platform.entity.Room;
import com.project.platform.mapper.CheckinApplicationMapper;
import com.project.platform.mapper.ElderMapper;
import com.project.platform.mapper.RoomMapper;
import com.project.platform.service.CheckinApplicationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.project.platform.vo.PageVO;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ${table.name}
 */
@Service
public class CheckinApplicationServiceImpl  implements CheckinApplicationService {
    @Resource
    private CheckinApplicationMapper checkinApplicationMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private ElderMapper elderMapper;
    
    @Override
    public PageVO<CheckinApplication> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        PageVO<CheckinApplication> page = new PageVO();
        List<CheckinApplication> list = checkinApplicationMapper.queryPage((pageNum - 1) * pageSize, pageSize, query);
        page.setList(list);
        page.setTotal(checkinApplicationMapper.queryCount(query));
        return page;
    }

    @Override
    public CheckinApplication selectById(Integer id) {
        CheckinApplication checkinApplication = checkinApplicationMapper.selectById(id);
        return checkinApplication;
    }

    @Override
    public List<CheckinApplication> list() {
        return checkinApplicationMapper.list();
    }
    @Override
    public void insert(CheckinApplication entity) {
        check(entity);
        checkinApplicationMapper.insert(entity);
    }
    @Override
    public void updateById(CheckinApplication entity) {
        check(entity);
        checkinApplicationMapper.updateById(entity);
    }
    private void check(CheckinApplication entity) {

    }
    @Override
    public void removeByIds(List<Integer> ids) {
        checkinApplicationMapper.removeByIds(ids);
    }

    @Override
    public CheckinApplication getByElder(Integer id) {
        return checkinApplicationMapper.getByElder(id);
    }

    @Override
    public void approve(CheckinApplication entity) {
        check(entity);
        checkinApplicationMapper.updateById(entity);
        if(Objects.nonNull(entity.getRoomId())){
            Room room =  roomMapper.selectById(entity.getRoomId());
            Elder elder =  elderMapper.selectById(entity.getElderId());
            elder.setRoomId(entity.getRoomId());
            elder.setBuildingId(room.getBuildingId());
            elder.setCheckinDate(entity.getProcessTime());
            elderMapper.updateById(elder);
        }
    }


}
